<?php
namespace app\home\model;

use PullWord\Facade\PullWord;
use think\Model;

class Demand extends Model
{
    protected $pk = 'id';
    protected $schema = [
        'id'            =>  'int',
        'title'         =>  'string',
        'type'          =>  'string',
        'add_time'      =>  'datetime',
        'size'          =>  'string',    
        'origin'        =>  'string',
        'price'         =>  'float',
        'rec'           =>  'int',
        'update_time'   => 'datetime',
        'hash_str'      =>  'string',
        'detail_text'   => 'string',
        'unit'          => 'string',
    ];
    public function getAddTimeAttr($value)
    {
        return get_date($value);
    }
    public function getUpdateTimeAttr($value)
    {
        return get_date($value);
    }
    public function getDescAttr($value, $data){
        $desc = '';
        $d_desc['content'] = trim(strip_tags($data['detail_text']));
        $d_desc['len'] = mb_strlen($d_desc['content']);
        if($d_desc['len'] < 8){
            $desc = $d_desc['content'].Seo::getDesc();
        }else{
            $desc = $d_desc['content']."【真木网】";
        }
        return $desc ;
    }
    public function getKwAttr($value, $data){
        $kws = (array)json_decode(PullWord::pull($data['title'])->toJson()->get());
        $str = '';
        if(count($kws) >= 8){
            foreach($kws as $index => $kw){
                $str .= $kw->t;
                if ($index+1 < count($kws)){
                    $str .= ", ";
                }
            }
        }else{
            foreach($kws as $index => $kw){
                $str .= $kw->t.', ';
            }
            $str .= Seo::getKeywords();
        }
        return $str;
    }
    public static function getByOrder($order="date", $limit=5)
    {
        $order_option = [
            'date'          =>  'add_time',
            'date_desc'     =>  'add_time desc',    
            'price_low'     =>  'price',
            'price_hight'   =>  'price desc'
        ];
        return self::order($order_option[$order])
                    ->where("title is not null or title != ''")
                    ->limit($limit)
                    ->select();
    }

}
